var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: -1,
        imageList: [],
        // 上传链接
        uploadUrl: url.uploadImage,
        title: '',
        content: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ id: options.id });
    },

    onImagePreview(e) {
        const { file, fileList } = e.detail;
        wx.previewImage({
            current: file.url,
            urls: fileList.map((n) => n.url),
        })
    },

    onImageComplete(e) {
        const { fileList } = e.detail
        wx.hideLoading();
    },

    onImageChange(e) {
        const { file, fileList } = e.detail
        if (file.status === 'uploading') {
            wx.showLoading({ mask: true });
        } else if (file.status === 'done') {
            this.setData({ imageList: fileList });
        }
    },
    onImageRemove(e) {
        const { file, fileList } = e.detail;
        this.setData({
            imageList: fileList.filter((n) => n.uid !== file.uid),
        })
    },
    onChangeValue(e) {
        let name = e.currentTarget.dataset.name;
        let value = e.detail.detail.value;

        let data = {};
        data[name] = value;
        this.setData(data);
    },
    onRelease() {
        let that = this;
        let params = {};
        let images = [];
        images = this.data.imageList.map((n) => {
            let data = JSON.parse(n.res.data);
            return data.data.url;
        });
        if (this.data.title == '') {
            wx.showModal({
                title: '提示',
                content: '标题不能为空',
                success(res) { }
            })
            return false;
        }

        if (images.length == 0) {
            wx.showModal({
                title: '提示',
                content: '请选择图片',
                success(res) { }
            })
            return false;
        }
        params.circle_photo_type_id = this.data.id;
        params.title = this.data.title;
        params.content = this.data.content;
        params.resource_type = 'image';
        params.resource = images.join(',');

        wx.showLoading({ mask: true });
        common.post(url.addCirclePhoto, params, (res) => {
            wx.hideLoading();
            wx.showModal({
                title: '提示',
                content: '新增成功',
                success(res) {
                    wx.navigateBack({
                        delta: 1
                    });
                }
            })
        });
    }


})